/*
* acooly.cn Inc.
* Copyright (c) 2017 All Rights Reserved.
* create by ouwen
* date:2017-09-20
*/
package com.yiji.fintech.musfinance.web;

import com.acooly.core.common.web.AbstractJQueryEntityController;
import com.acooly.core.common.web.MappingMethod;
import com.acooly.core.utils.Collections3;
import com.yiji.fintech.musfinance.common.enums.StatusEnum;
import com.yiji.fintech.musfinance.entity.CooperationInstBank;
import com.yiji.fintech.musfinance.service.AreaCodeService;
import com.yiji.fintech.musfinance.service.CooperationInstBankService;
import com.yiji.fintech.musfinance.service.InstBankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * center_cooperation_inst_bank 管理控制器
 * 
 * @author ouwen
 * Date: 2017-09-20 10:58:33
 */
@Controller
@RequestMapping(value = "/manage/cooperationbank/cooperationInstBank")
public class CooperationInstBankManagerController extends AbstractJQueryEntityController<CooperationInstBank, CooperationInstBankService> {
	

	{
		allowMapping = "*";
	}
	@Autowired
	private InstBankService instBankService;
	@Autowired
	private AreaCodeService areaCodeService;

	@Override
	protected CooperationInstBank doSave(HttpServletRequest request, HttpServletResponse response, Model model, boolean isCreate) throws Exception {
		List<Map<String,Object>> allBank = instBankService.getAllInstBank();
		CooperationInstBank cooperationInstBank = loadEntity(request);
		if(cooperationInstBank==null){
			this.allow(request, response, MappingMethod.create);
			cooperationInstBank = new CooperationInstBank();
		}else{
			this.allow(request, response, MappingMethod.update);
		}
		this.doDataBinding(request, cooperationInstBank);
		this.onSave(request, response, model, cooperationInstBank, isCreate);

		CooperationInstBank finalCooperationInstBank = cooperationInstBank;
		List<Map<String,Object>> result = allBank.stream().filter((s)->{
			Set<Map.Entry<String,Object>> entrySet =  s.entrySet();
			Iterator<Map.Entry<String,Object>> iterator = entrySet.iterator();
			while (iterator.hasNext()){
				Map.Entry<String,Object> entry = iterator.next();
				if(String.valueOf(entry.getValue()).equals(finalCooperationInstBank.getInstBankId())){
					return true;
				}
			}
			return false;
		}).collect(Collectors.toList());
		if(Collections3.isNotEmpty(result)){
			Map<String,Object> map =  result.get(0);
			cooperationInstBank.setCoInstBankName((String)map.get("INST_BANK_NAME"));
		}else{
			throw new IllegalArgumentException("银行编码【"+finalCooperationInstBank.getInstBankId()+"】基础信息未配置");
		}
		if(isCreate) {
			this.getEntityService().save(cooperationInstBank);
		}else{
			this.getEntityService().update(cooperationInstBank);
		}
		return cooperationInstBank;
	}

	@Override
	protected void referenceData(HttpServletRequest request, Map<String, Object> model) {
		model.put("allStatuss", StatusEnum.mapping());
		List<Map<String,Object>> allBank = instBankService.getAllInstBank();
		model.put("allBank", allBank);
		List<Map<String,Object>> allArea = areaCodeService.getAllArea();
		model.put("allArea", allArea);
	}
	

}
